package com.ag.zhh.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ag.bean.TbDeptType;
import com.ag.zhh.vo.Dept;
import com.ag.zhh.vo.UserDept;

/**
 * Dept的数据访问对象
 * 
 * @author 张颢铧
 * 
 */

public class DeptDAO extends HibernateDaoSupport {

	private static final Log log = LogFactory.getLog(DeptDAO.class);

	protected void initDao() {
		// do nothing
	}

	public void setDeptDIfOnly(List<String> DIds) throws Exception {
		accessDeptDIf(DIds, 1);
	}

	public void resetDeptDIfOnly(List<String> DIds) throws Exception {
		accessDeptDIf(DIds, 0);
	}

	

	public List<Dept> findByDIf(int DIf) throws Exception {
		Session session = null;
		List<Dept> list = null;
		try {
			session = this.getSessionFactory().openSession();

			Query query = session.createQuery("from Dept where DIf=?");
			query.setInteger(0, DIf);
			list = query.list();
		} catch (Exception e) {
			throw e;
		} finally {
			if (session != null) {
				session.close();
			}
			
		}System.out.println("********长度：**********"+list.size());
		return list;
	}

	public List<Dept> findAll()throws Exception{
		List<Dept> result=null;
		Session session  =  this.getSessionFactory().openSession();
		try{
			
			result = session.createQuery("from Dept").list();
			System.out.println("DeptDAO: **********************************************");
			System.out.println("部门数目: "+(result!=null?result.size():result));
			
		}catch(Exception e){e.printStackTrace();}
		finally{
		session.close();
		}
		
		return result;
		
	}

	public void deleteDept(Dept dept) {
		log.debug("saving TbDept instance");
		try {
			getHibernateTemplate().delete(dept);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	public void deleteDept(String  deptId) {
		log.debug("saving TbDept instance");
		Session session = this.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();
		try {
			session.createSQLQuery("delete from TB_DEPT_AND_CONTACT t where t.D_ID =:did").setString("did", deptId).executeUpdate();
			session.createQuery("delete from Dept d where d.DId =:did").setString("did", deptId).executeUpdate();
			
			tx.commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			tx.rollback();
			log.error("save failed", re);
			throw re;
		}
		session.close();
	}
	
	
	private void accessDeptDIf(List<String> DIds, int value) throws Exception {
		Session session = null;
		Transaction tx = null;
		String hql = "update Dept d set d.DIf = " + value + "where d.DId = :DId";
		try {
			session = this.getSessionFactory().openSession();
			tx = session.beginTransaction();
			for(String DId:DIds) {
				Query query = session.createQuery(hql);
				query.setString("DId", DId);
				
				query.executeUpdate();
			}
			//System.out.println("**************" + "Betch Update!!");
			tx.commit();
		} catch (Exception e) {
			if(tx != null) {
				tx.rollback();
			}
			throw e;
		}finally {
			if(session != null) {
				session.close();
			}
		}
	}
	
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Dept instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from TbDept as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public void saveOrUpdateDept(Dept dept) {
		// TODO Auto-generated method stub		
	}
	public void saveOrUpdateDept(UserDept dept) {
		Session session = this.getSessionFactory().openSession();
		
		Transaction tx = session.beginTransaction();
		session.update(dept);
		tx.commit();
		
		session.close();
	}
}

